define(["jquery","login","register"],function($){
	function Header(){
		this.loginTempolate = `<nav class="navbar navbar-inverse">
									<div class="container-fluid">
										<div class="navbar-header">
											<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
												<span class="icon-bar"></span>
												<span class="icon-bar"></span>
												<span class="icon-bar"></span>
											</button>
											<a class="navbar-brand" href="#">拉勾网管理系统</a>
										</div>
										<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
											<ul class="nav navbar-nav">
												<li><a href="/">首页</span></a></li>
												<li><a href="/html/position.html">职位管理</a></li>
											</ul>
											<ul class="nav navbar-nav navbar-right not-login">
												<li class="log"><a href="#" data-toggle="modal" data-target="#ModalLogin">登录</a></li>
												<li><a href="#" data-toggle="modal" data-target="#ModalRegister">注册</a></li>
											</ul>
											<ul class="nav navbar-nav navbar-right hidden already-login">
												<li><a href="#" class="link-username">username</a></li>
												<li><a href="#" class="link-logout">退出</a></li>
											</ul>
										</div>
									</div>
								</nav>`
		
		this.createDom().loadUser().addListener();
	}
	$.extend(Header.prototype,{
		//渲染header
		createDom(){
			$(this.loginTempolate).appendTo("header")
			return this;
		},
		//加载sessionStorage的登录信息
		loadUser(){
			const username = sessionStorage.username;
			if (username) {
				console.log(sessionStorage)
				$(".already-login").removeClass("hidden")
												.find(".link-username").text(username).end()
												.siblings(".not-login").addClass("hidden");
			}
			return this;

		},
		//给登录、退出按钮注册事件监听
		addListener(){
			$(".log").on("click",this.logHandle);
			$(".link-logout").on("click",this.logoutHandle);
		},
		logoutHandle(){
			/*const url =  "http://rap2api.taobao.org/app/mock/119565/loginout"
			$.get(url,res=>{
				console.log(res)
				sessionStorage.clear();
				location = "/";
			})*/
			sessionStorage.clear();
			//location = "/";
			window.location.reload();
		},
		logHandle(){
			if (sessionStorage.password) {
				$(".username").val(sessionStorage.username);
				$(".password").val(sessionStorage.password);
			}
		}
	})
	return new Header();
})